package com.cn.demo.controller;

import com.cn.demo.entity.Organization;
import com.cn.demo.entity.Student;
import com.cn.demo.entity.Teacher;
import com.cn.demo.service.StudentService;
import com.cn.demo.utils.Result;
import com.cn.demo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping(value = "/add")
    public Result add(@RequestBody Student student) {
        if (!StringUtils.isPhone(student.getTelephone())){
           return Result.error("手机格式错误");
        }
        return studentService.add(student);
    }

    @GetMapping(value = "/get")
    public Result get(@RequestParam(value = "page",defaultValue = "1")Integer page, @RequestParam(value = "limit",defaultValue = "5")Integer limit,
                      @RequestParam(value = "studentId",required = false)String studentId,
                      @RequestParam(value = "studentName",required = false)String studentName,
                      @RequestParam(value = "telephone",required = false)String telephone
                      ){
        Student student = new Student();
        student.setStudentId(studentId);
        student.setStudentName(studentName);
        student.setTelephone(telephone);
        return studentService.get(page,limit,student);
    }

    @GetMapping(value = "/getByStudentId/{studentId}")
    public Result get(@PathVariable(value = "studentId") String studentId){
        return studentService.getByStudentId(studentId);
    }

    @PostMapping(value = "/delete")
    public Result delete(@RequestBody String[] studentIdList){
        if (studentIdList.length == 0){
            return Result.error();
        }
        return studentService.delete(studentIdList);
    }


    @PutMapping(value = "/update")
    public Result update(@RequestBody Student student) {
        if (!StringUtils.isPhone(student.getTelephone())){
            return Result.error("手机格式错误");
        }
        return studentService.update(student);
    }

}